package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

//验证绑定的结构体
func main() {
	r := gin.Default()
	r.GET("/validatestruct", func(c *gin.Context) {
		var person Person
		if err := c.ShouldBind(&person); err != nil { //参数绑定到结构体中
			c.String(http.StatusBadGateway, "error : %v", err)
			c.Abort()
			return
		}
		c.String(http.StatusOK, "person :%v", person)
	})
	r.Run(":8081")
}

type Person struct {
	Age     int    `form:"age" binding:"required,gt=10"` //根据结构体的绑定验证来是否产生err，验证失败产生绑定失败的err，必须按照验证要求绑定即可
	Name    string `form:"name" binding:"required"`
	Address string `form:"address" binding:"required"`
}
